#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：16.请求重试案例.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-20 14:39
'''
import requests
from retrying import retry

num = 1

@retry(stop_max_attempt_number=3)
def parse_html(url):
    print("开始")
    global num
    print(f"第{num}次请求尝试......")
    num += 1

    headers = {
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
    }
    response = requests.get(url, headers=headers, timeout=3, allow_redirects=False)
    print("请求完成")
    print(response)
    # 断言
    assert response.status_code == 200
    return response


def main():
    try:
        url = "https://www.360buy.com"
        response = parse_html(url)
        print(response)
    except Exception as e:
        print(f"请求失败：{e}")

main()